#!/bin/bash

set -e

ACTION=$1

function is_steam_gamepad_running {
    pid_file="$HOME/.steam/steam.pid"
    if [ -f "$pid_file" ]; then
        pid=$(cat $pid_file)
        if [ -n "$pid" ]; then
            cmd=$(cat /proc/$pid/cmdline | tr '\0' ' ')
            if [[ "$cmd" =~ "-gamepadui" ]]; then
                return 0
            fi
        fi
    fi
    return 1
}

function init {
    sudo waydroid init -f -c 'https://ota.waydro.id/system' -v 'https://ota.waydro.id/vendor' -s GAPPS
    sudo waydroid-extras install libhoudini
    echo "Waydroid has been initialized, please run waydroid once before you Configure Waydroid"
}

function configure {
    sudo /usr/bin/waydroid-extras
}

function reset {
    echo "Resetting Waydroid"
    bash -c 'sudo rm -rf /var/lib/waydroid /home/.waydroid ~/waydroid ~/.share/waydroid ~/.local/share/applications/*aydroid* ~/.local/share/waydroid'
    echo "Waydroid has been reset"
}

case $ACTION in
    init)
        init
        ;;
    configure)
        configure
        ;;
    reset)
        reset
        ;;
    *)
        echo "Usage: $0 {init|configure|reset}"
        exit 1
        ;;
esac